package ai.people.netmon.warning.controller;

import ai.people.core.common.util.ApplicationContextUtil;
import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.domain.warning.entity.WarningInfoMaterial;
import ai.people.netmon.framework.domain.warning.request.collect.MessageContentSaveVO;
import ai.people.netmon.framework.domain.warning.request.material.MaterialSaveRequest;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.warning.dict.DictGrpcClient;
import ai.people.netmon.warning.service.WarningInfoMaterialService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 素材相关接口
 *
 * @author yuanqinglong
 * @date 2022/7/27 14:40
 */
@Validated
@RestController
@RequestMapping("/v1/material")
public class WarningMaterialController {

    @Autowired
    private WarningInfoMaterialService warningInfoMaterialService;

    /**
     * 添加/删除素材
     *
     * @param type           操作类型，1添加，2删除
     * @param materialSource 收集来源 1:信息预警、2:主题舆情、3:账号言论
     * @param reqParam       要求参数
     * @return {@link Result}<{@link Integer}>
     */
    @PostMapping("/{type}/{materialSource}")
    public Result<Integer> add(@PathVariable("type") Integer type,
                                    @PathVariable(value = "materialSource", required = false) Integer materialSource,
                                    @Validated @RequestBody MaterialSaveRequest reqParam) {

        DictGrpcClient dictService = ApplicationContextUtil.getBean(DictGrpcClient.class);
        Map<String, String> platformNameMap = dictService.getDictMap("warning_message_source");
        HashMap<String, String> newPlatformNameMap = Maps.newHashMap();
        platformNameMap.keySet().forEach(o-> newPlatformNameMap.put(platformNameMap.get(o), o));

        if(reqParam.getMessageContent() == null){
            MessageContentSaveVO saveVO = new MessageContentSaveVO();
            saveVO.setId(reqParam.getId());
            saveVO.setPlatform(newPlatformNameMap.getOrDefault(reqParam.getPlatform(), reqParam.getPlatform()));
            reqParam.setMessageContent(saveVO);
        }

        WarningInfoMaterial infoMaterial = WarningInfoMaterial.builder()
                .materialUserId(RequestHolderUtil.getUserId())
                .materialInfoPlatform(reqParam.getMessageContent().getPlatform())
                .materialInfoId(reqParam.getMessageContent().getId())
                .materialSource(materialSource)
                .materialInfoAccountTitle(reqParam.getMessageContent().getTitle())
                .materialInfoContent(reqParam.getMessageContent().getContent())
                .materialInfoEmotion(reqParam.getMessageContent().getEmotion())
                .materialInfoLabel(reqParam.getMessageContent().getLabel())
                .materialInfoReleaseTime(reqParam.getMessageContent().getTime())
                .build();
        if(type == 1){
            return ResultUtil.success(warningInfoMaterialService.addMaterial(infoMaterial, JSON.toJSONString(reqParam.getMessageContent())));
        }else if(type == 2){
            return ResultUtil.success(warningInfoMaterialService.deleteMaterial(infoMaterial));
        }
        return ResultUtil.success(0);
    }

    /**
     * 批量添加素材
     *
     * @param materialSource 收集来源 1:信息预警、2:主题舆情、3:账号言论
     * @param list       要求参数
     * @return {@link Result}<{@link Integer}>
     */
    @PostMapping("/batch-add/{materialSource}")
    public Result<Integer> batchSave(@PathVariable(value = "materialSource", required = false) Integer materialSource,
                                    @Validated @RequestBody List<MaterialSaveRequest> list) {
        return ResultUtil.success(warningInfoMaterialService.batchSave(materialSource, list));
    }
}
